import Taro, { Component } from '@tarojs/taro'
import { AtList, AtListItem } from 'taro-ui'
import { View } from '@tarojs/components'
import { DNavBar } from '@/components'
import http from '@/api'
import sdk from '@/common/sdk'
import './inventoryed.scss'
import dayjs from 'dayjs'
export default class inventoryed extends Component {

  constructor(props) {
    super(props)
    this.state = {
      type: 0, // 0:待盘库 1:已盘库
      id: "",
      info: '',
      darr: [],
      detail1: [],
      detail2: []
    }
  }


  componentWillMount() {
    let { type, id } = this.$router.params
    if (type === '0') {
      this.getWaitDetail()
    } else {
      this.getDetail()
    }
    // this.getDetaiInfo(id)
    this.setState({
      type,
      id
    })
  }

  componentDidShow() {
    const { type } = this.state
    sdk.setTitleInfo(this.getTitle(type))
  }


  getTitle(type) {
    return {
      $0: '待盘库',
      $1: '已盘库'
    }[`$${type}`]
  }

  // getDetaiInfo(id) {
  //   http.getInvenDetail({ id }).then(res => {
  //     // console.log("res-->", res)
  //     // debugger
  //   })
  // }

  onHandleDetail(item, type) {
    let _objUrl = {
      0: `/pages/dwaitinventory/dwaitinventory?id=${this.$router.params.id}&refDetailId=${item.id}`,
      1: `/pages/dinventoryed/dinventoryed?id=${this.$router.params.id}&date=${item.stocktakingDate}`
    }
	if(dayjs(item.stocktakingDate).date()==dayjs().date()){
		Taro.navigateTo({
		  url: _objUrl[type]
		})
	}else{
		Taro.showToast({
			text:'只能对当天的进行盘库'
		})
	}

  }

  getDetail() {
    const id = this.$router.params.id
    let _obj = {
      id
    }
    http.getAppStocktaking(_obj).then(res => {
      let { detail1 } = this.state
      res.detail.map(item => {
        if (item.status === 2) {
          detail1.push({
            item,
            status: 1
          })
        }
      })
      this.setState({
        info: res.info,
        detail: detail1
      })
    })
  }
  getWaitDetail() {
    const id = this.$router.params.id
    let _obj = {
      id
    }
    http.getAppStocktaking(_obj).then(res => {
      // console.log(778, res)
      let { detail1 } = this.state
      res.detail.map(item => {
        if (item.status === 1) {
          detail1.push({
            item: item,
            status: 0
          })
        } else if (item.status === 2) {
          detail1.push({
            item: item,
            status: 1
          })
        }
      })
      this.setState({
        info: res.info,
        detail: detail1
      })
    })
  }
  getMonthDay(date) {
    return date.split('-')[1] + '月' + date.split('-')[2] + '日'
  }
  render() {
    const {
      type,
      info,
      detail
    } = this.state
    return (
      <View className='b_inventoryed'>
        <DNavBar title={this.getTitle(type)} className='b_inventoryed_nav'></DNavBar>
        <View className='b_inventoryed__box'>
          <View className='b_inventoryed__box_flex'>
            <View className='b_inventoryed__box_left'>
              <View>车型</View>
              <View>车架号</View>
              <View>外观/内饰</View>
            </View>
            <View className='b_inventoryed__box_right'>
              <View>{!info.carType ? '---' : info.carType}</View>
              <View>{!info.vin ? '---' : info.vin}</View>
              <View>{info.outColor + '/' + info.inColor}</View>
            </View>
          </View>
          <AtList className='b_inventoryed__list'>
            <View className='b_inventoryed__list_titile'>盘库状态</View>
            {
              detail && detail.map((item, i) => {
                return <AtListItem
                  key={i}
                  onClick={this.onHandleDetail.bind(this, item.item, item.status)}
                  arrow='right'
                  title={this.getMonthDay(item.item.stocktakingDate) + '盘库'}
                  extraText={this.getTitle(item.status)}
                />
              })
            }
          </AtList>
        </View>
      </View>

    )
  }

}
